package com.newzqxq.koalamoney.ui;


import android.Manifest;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RadioButton;

import com.newzqxq.koalamoney.R;
import com.newzqxq.koalamoney.common.BaseActivity;
import com.newzqxq.koalamoney.common.PermissionListener;
import com.newzqxq.koalamoney.entity.UserBean;
import com.newzqxq.koalamoney.presenter.MainPresenter;
import com.newzqxq.koalamoney.ui.fragment.HomeFragment;
import com.newzqxq.koalamoney.ui.fragment.MoneyFragment;
import com.newzqxq.koalamoney.ui.fragment.MyFragment;
import com.newzqxq.koalamoney.utils.StatusBarUtil;
import com.newzqxq.koalamoney.view.IMainView;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseActivity implements IMainView {
    private static final int PER_REQUEST_CODE = 10;
    private Activity activity;
    MainPresenter presenter;

    @BindView(R.id.bottom_bar_home)
    RadioButton homeBar;//首页
    @BindView(R.id.bottom_bar_money)
    RadioButton moneyBar;//理财
    @BindView(R.id.bottom_bar_my)
    RadioButton myBar;//我的

    private HomeFragment homeFragment;
    private MoneyFragment moneyFragment;
    private MyFragment myFragment;
    private Fragment[] fragments;
    private int index;
    private int currentTabIndex;// 当前fragment的index

    @Override

    protected int getContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initData() {
        StatusBarUtil.setStatusBarColor(this, R.color.colorPrimary);
        myRequestPermissions("为了更好的为您服务，我们需要以下必要权限", new String[]{Manifest.permission.READ_PHONE_STATE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE}
                , PER_REQUEST_CODE, new PermissionListener() {
                    @Override
                    public void onPermissionGranted() {

                    }
                });
    }

    @Override
    protected void initView() {
        activity = this;
        presenter = new MainPresenter(this);

        homeFragment = new HomeFragment();
        moneyFragment = new MoneyFragment();
        myFragment = new MyFragment();
        fragments = new Fragment[]{homeFragment, moneyFragment, myFragment};
        homeBar.setChecked(true);
        getSupportFragmentManager().beginTransaction()
                .add(R.id.layFrame, homeFragment)
                .add(R.id.layFrame, moneyFragment)
                .add(R.id.layFrame, myFragment)
                .hide(homeFragment)
                .hide(moneyFragment)
                .hide(myFragment)
                .show(homeFragment).commit();
    }


    @Override
    protected void initListener() {

    }

    @Override
    protected void clearMemory() {

    }

    @Override
    public void getResult(UserBean str) {
        if (str != null) {
            UserBean.Author author = str.author;
        }
    }

    @Override
    public void getErrorResult(String message) {

    }

    @OnClick({R.id.bottom_bar_home, R.id.bottom_bar_money, R.id.bottom_bar_my})
    public void onClick(View view) {
        resetIcon();
        switch (view.getId()) {
            case R.id.bottom_bar_home:
                homeBar.setTextColor(getResources().getColor(R.color.red_f4333c));
                index = 0;
                break;
            case R.id.bottom_bar_money:
                moneyBar.setTextColor(getResources().getColor(R.color.red_f4333c));
                index = 1;
                break;
            case R.id.bottom_bar_my:
                myBar.setTextColor(getResources().getColor(R.color.red_f4333c));
                index = 2;
                break;
        }
        if (currentTabIndex != index) {
            FragmentTransaction trx = getSupportFragmentManager()
                    .beginTransaction();
            trx.hide(fragments[currentTabIndex]);
            if (!fragments[index].isAdded()) {
                trx.add(R.id.layFrame, fragments[index]);
            }
            trx.show(fragments[index]).commit();
        }
        currentTabIndex = index;
    }

    private void resetIcon() {
        homeBar.setTextColor(getResources().getColor(R.color.colorTextGary));
        moneyBar.setTextColor(getResources().getColor(R.color.colorTextGary));
        myBar.setTextColor(getResources().getColor(R.color.colorTextGary));
    }

}
